from django.db import models
from user.models import CustomUser

# Create your models here.

class Channel(models.Model):
    title = models.CharField(verbose_name='频道', max_length=30, unique=True)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "频道"
        verbose_name_plural = "频道"


class Category(models.Model):
    title = models.CharField(verbose_name="分类名", max_length=20, unique=True)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "分类名"
        verbose_name_plural = "分类名"


class WordCount(models.Model):
    title = models.IntegerField(verbose_name='字数', default=0)

    def __str__(self):
        return str(self.title)

    class Meta:
        verbose_name = "字数"
        verbose_name_plural = "字数"


class Image(models.Model):
    title = models.CharField(verbose_name='书籍封面', max_length=20)
    image = models.ImageField(verbose_name='书籍图片', upload_to='img/')

    def __str__(self):
        return self.title


class Author(models.Model):
    title = models.CharField(verbose_name='作者', max_length=20, unique=True)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "作者"
        verbose_name_plural = "作者"


class Chapter(models.Model):
    title = models.CharField(verbose_name='章节', max_length=50)

    def __str__(self):
        return self.title


class Book(models.Model):
    title = models.CharField(verbose_name='书名', max_length=30, unique=True)
    synopsis = models.TextField(verbose_name='简介', max_length=500)
    cover = models.ImageField(verbose_name='封面连接', upload_to='cover')
    image = models.ForeignKey(verbose_name='书籍封面', to=Image, on_delete=models.CASCADE)
    word_count = models.ForeignKey(verbose_name='字数', to=WordCount, on_delete=models.CASCADE, blank=True, null=True)
    click = models.IntegerField(verbose_name='点击量', default=0)
    author = models.ForeignKey(verbose_name='作者', to=Author, on_delete=models.CASCADE)
    category = models.ForeignKey(verbose_name='分类', to=Category, on_delete=models.CASCADE)
    channel = models.ForeignKey(verbose_name='频道', to=Channel, on_delete=models.CASCADE, blank=True, null=True)
    update_time = models.DateTimeField(verbose_name='更新时间', auto_now=True, blank=True, null=True)
    chapter = models.ManyToManyField(verbose_name='书籍章节', to=Chapter)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "书名"
        verbose_name_plural = "书名"


class Ads(models.Model):
    title = models.CharField(verbose_name="轮播图", unique=True, max_length=30)
    url = models.URLField(verbose_name='图片连接地址', unique=True)
    img = models.ImageField(verbose_name="图片", upload_to="ads/")

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "轮播图"
        verbose_name_plural = "轮播图"

class Comment(models.Model):
    chapter = models.ForeignKey('Chapter', on_delete=models.CASCADE, related_name='comments')
    user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.user.username}: {self.content[:20]}"